package com.example.filerwdemo;

import android.content.Context;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Created by hp on 2018/6/12.
 */

public class FileSavePwd {
    static String fileName="data.txt";
    public static void saveInfo(Context ctx, Map<String, String> infoMap)
    {
        FileOutputStream out=null;
        BufferedWriter bufWriter=null;

        try {
            out=ctx.openFileOutput(fileName,Context.MODE_APPEND);
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (bufWriter!=null)
            {
                try {
                    bufWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out!=null)
            {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static Map<String,String> readInfo(Context ctx){
        FileInputStream in=null;
        BufferedReader bufReader=null;
        Map<String,String> infoMap=new HashMap<String, String>();
        try {
            in=ctx.openFileInput(fileName);
            bufReader= new BufferedReader(new InputStreamReader(in));
            String line="";
            while ((line = bufReader.readLine()) != null) {
                    String[] infos = line.split(":");
                    infoMap.put(infos[0], infos[1]);
                }

        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (bufReader!=null)
            {
                try {
                    bufReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in!=null)
            {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return infoMap ;

    }
}
